package com.easy.easylibdemo.ui.greendao;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.view.View;

import com.easy.easylibdemo.R;
import com.easy.easylibdemo.gen.SearchRecordBeanDao;
import com.easy.easylibdemo.ui.greendao.adapter.SearchRecordAdapter;
import com.easy.easylibdemo.ui.greendao.model.SearchRecordBean;
import com.easy.easylibdemo.ui.greendao.model.SearchRecordImpl;
import com.easy.easylibdemo.ui.greendao.model.SearchRecordModel;
import com.easy.easylibdemo.ui.greendao.utils.GreenDaoUtils;
import com.easylib.base.BaseActivity;
import com.easylib.utils.ToastUtils;
import com.easylib.views.DividerItemDecoration;
import com.guyj.listener.EasyOnItemChildClickListener;

import java.util.List;

import butterknife.Bind;


public class GreenDaoActivity extends BaseActivity {

    @Bind(R.id.search)
    SearchView search;
    @Bind(R.id.recycler)
    RecyclerView recycler;
    private SearchRecordBeanDao searchRecordBeanTable;
    private LinearLayoutManager linearLayoutManager;
    private SearchRecordAdapter adapter;
    private List<SearchRecordBean> searchRecordBeans;
    private List<SearchRecordBean> filterRecordBeans;
    private List<SearchRecordBean> resultBeans;
    private SearchRecordBean bean;
    private SearchRecordModel searchRecordModel;

    @Override
    public int getContentViewId() {
        return R.layout.activity_green_dao;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        search.setIconified(true);//false展开 true关闭
        search.setIconifiedByDefault(false);//查询图标默认位于编辑框true内 false外
        search.setSubmitButtonEnabled(true);
    }

    @Override
    protected void initVariable() {
        searchRecordModel=new SearchRecordImpl();
        searchRecordBeans=searchRecordModel.getSearchRecords();
        searchRecordBeanTable = GreenDaoUtils.getInstance().getDaoSession().getSearchRecordBeanDao();
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        recycler.setLayoutManager(linearLayoutManager=new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
        recycler.setAdapter(adapter=new SearchRecordAdapter(mContext,R.layout.item_search_record,searchRecordBeans));
        recycler.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        adapter.setEasyOnItemChildClickListener(new EasyOnItemChildClickListener() {
            @Override
            public void onClick(View view, int i) {
                switch (view.getId()){
                    case R.id.tv_name:
                        //调用搜索，隐藏历史记录;
                        search.setQuery(adapter.getDatas().get(i).getSearchRecord(),true);
                        break;
                    case R.id.iv_delete:
                        //删除某条记录，刷新列表
                        deleteBeanAndNotify(i);
                        break;
                }
            }
        });
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                //提交搜索，加入历史记录，重复则不添加，隐藏记录列表
                ToastUtils.showToast("search "+query);
                searchRecordBeanTable.insertOrReplaceInTx(bean=new SearchRecordBean(null,query));
//                filterAdd(query);
                recycler.setVisibility(View.GONE);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                //输入框文字变化，为空显示全部记录，变化显示过滤后记录
                recycler.setVisibility(View.VISIBLE);
                if (TextUtils.isEmpty(newText)){
                    adapter.setFilter(false);
                    adapter.setDatas(searchRecordBeans);
                    recycler.scrollToPosition(0);
                }else {
                    //过滤历史记录列表，匹配输入内容
                    filterRecordBeans=searchRecordModel.filterQuery(searchRecordBeans,newText);
                    adapter.setFilter(true);
                    adapter.setDatas(filterRecordBeans);
                    recycler.scrollToPosition(0);
                }
                return false;
            }
        });
    }

    /**
     * 删除and刷新列表
     * @param i
     */
    private void deleteBeanAndNotify(int i) {
        if (i>-1){
            searchRecordBeanTable.delete(searchRecordBeans.get(i));
            searchRecordBeans.remove(i);
            adapter.notifyItemRemoved(i);
            if (i!=searchRecordBeans.size()){
                adapter.notifyItemRangeChanged(i,searchRecordBeans.size()-i);
            }
        }
    }


    /**
     * 内容已存在则不添加
     * @param addStr
     * @return
     */
//    private void filterAdd(String addStr){
//        for (SearchRecordBean bean:searchRecordBeans
//                ) {
//            if (bean.getSearchRecord().contains(addStr)){
//                break;
//            }
//        }
//        resultBeans.add(new SearchRecordBean(null,addStr));
//    }

}
